package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.CustomsHandbookProductdetailExtendMapper;
import com.aiwei.tdjk.dto.request.CustomsHandbookProductdetailPagerRequest;
import com.aiwei.tdjk.dto.response.CustomsHandbookProductdetailResponse;
import com.aiwei.tdjk.entity.CustomsHandbookProductdetail;
import com.aiwei.tdjk.entity.CustomsHandbookProductdetailExample;
import com.aiwei.tdjk.service.CustomsHandbookProductdetailService;
import com.aiwei.tdjk.service.CustomsHandbookUsedDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomsHandbookProductdetailServiceImpl implements CustomsHandbookProductdetailService{

    @Autowired
    private CustomsHandbookProductdetailExtendMapper customsHandbookProductdetailExtendMapper;


    @Autowired
    private CustomsHandbookUsedDetailService customsHandbookUsedDetailService;


    /**
     * 分页查询
     * @param request
     * @return
     */
    @Override
    public Pager<CustomsHandbookProductdetailResponse> find(CustomsHandbookProductdetailPagerRequest request) {
        request.webParamConvert();
        List<CustomsHandbookProductdetailResponse> list = customsHandbookProductdetailExtendMapper.find(request);
        //list进行处理
        for (CustomsHandbookProductdetailResponse detail :list){
            int hasDeclareCountByDetailId = customsHandbookUsedDetailService.getHasDeclareCountByDetailId(detail.getId());
            int hasShangJianCountByDetailId = customsHandbookUsedDetailService.getHasShangJianCountByDetailId(detail.getId());
            detail.setHasDeclareQty(hasDeclareCountByDetailId);
            detail.setHasNotDeclareQty(hasShangJianCountByDetailId);
        }
        int totalCount = customsHandbookProductdetailExtendMapper.getTotalCount(request);
        Pager<CustomsHandbookProductdetailResponse> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(list);
        pager.setTotalCount(totalCount);
        return pager;
    }


    /**
     * 获取
     * @param id
     * @return
     */
    @Override
    public CustomsHandbookProductdetail get(Long id){
        return customsHandbookProductdetailExtendMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomsHandbookProductdetail> getByCustomsHandBook(Long id) {
        CustomsHandbookProductdetailExample example = new CustomsHandbookProductdetailExample();
        example.or().andCustomsHandBookIdEqualTo(id);
        return customsHandbookProductdetailExtendMapper.selectByExample(example);
    }

    @Override
    public CustomsHandbookProductdetail getByHandBookIdAndSpecifications(long handBookId, String specifications) {
        CustomsHandbookProductdetailExample example = new CustomsHandbookProductdetailExample();
        example.or().andCustomsHandBookIdEqualTo(handBookId).andSpecificationsEqualTo(specifications);
        List<CustomsHandbookProductdetail> productdetails = customsHandbookProductdetailExtendMapper.selectByExample(example);
        if (productdetails!=null&&productdetails.size()>0){
            return productdetails.get(0);
        }
        return null;
    }


    /**
     * 新增
     *
     * @param customsHandbookProductdetail
     * @return
     */
    @Override
    public int add(CustomsHandbookProductdetail customsHandbookProductdetail) {
        return customsHandbookProductdetailExtendMapper.insertSelective(customsHandbookProductdetail);
    }

    /**
     * 更新
     *
     * @param customsHandbookProductdetail
     */
    @Override
    public void update(CustomsHandbookProductdetail customsHandbookProductdetail) {
        customsHandbookProductdetailExtendMapper.updateByPrimaryKeySelective(customsHandbookProductdetail);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        customsHandbookProductdetailExtendMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id:ids){
            delete(id);
        }
    }

}
